package com.example.smartcitydemo.ui.subway.subway.fragment

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.example.smartcity_jscs.network.ResultStates.Resource
import com.example.smartcitydemo.R
import com.example.smartcitydemo.base.BaseFragment
import com.example.smartcitydemo.bean.CurrentStationDetail
import com.example.smartcitydemo.databinding.FragmentSubwayHomeBinding
import com.example.smartcitydemo.ext.init
import com.example.smartcitydemo.ext.onLoad
import com.example.smartcitydemo.ext.showJDPicker
import com.example.smartcitydemo.ui.subway.SubWayHomeAdapter
import com.example.smartcitydemo.ui.subway.SubWayServiceAdapter
import com.example.smartcitydemo.ui.subway.WeatherBannerAdapter
import com.example.smartcitydemo.ui.subway.bean.WeatherBannerBean
import com.example.smartcitydemo.viewModel.SubwayViewModel.SubWayViewModel
import com.lljjcoder.style.cityjd.JDCityPicker
import com.youth.banner.Banner
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.transformer.ZoomOutPageTransformer

/**
 * @PACKAGE_NAME: com.example.smartcitydemo.ui.subway.subway
 * @NAME: Caleb.F
 * @DATE: 2021/11/15 9:02
 * @Email: 1919796283@qq.com
 */
class SubWayHomeFragment : BaseFragment<FragmentSubwayHomeBinding>(R.layout.fragment_subway_home) {
    private val subwayViewModel: SubWayViewModel by viewModels()
    private val subWayServiceAdapter: SubWayServiceAdapter by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
        SubWayServiceAdapter().apply {
            setOnItemClickListener { adapter, view, pos ->
                when (pos) {
                    0 -> this@SubWayHomeFragment.findNavController()
                        .navigate(R.id.action_navigation_subway_home_to_navigation_subway_line)
                    1 -> this@SubWayHomeFragment.findNavController()
                        .navigate(R.id.action_navigation_subway_home_to_navigation_subway_image)
                    2 -> this@SubWayHomeFragment.findNavController()
                        .navigate(R.id.action_navigation_subway_home_to_navigation_subway_broadcast)
                    3 -> this@SubWayHomeFragment.findNavController()
                        .navigate(R.id.action_navigation_subway_home_to_navigation_subway_notice)

                }
            }
        }
    }

    private val subHomeAdapter: SubWayHomeAdapter by lazy {
        SubWayHomeAdapter().apply {
            init()
            setDiffCallback(CurrentStationDetail.diffCallBack)
        }
    }

    override fun initData() {
        subwayViewModel.getHomeBanner()
        subwayViewModel.getLocationInfo()
        subwayViewModel.getWeatherInfo()
        subwayViewModel.getHomeStationInfo()
    }

    override fun initView(bindView: FragmentSubwayHomeBinding, savedInstanceState: Bundle?) {
        bindView.run {
            //处理点击事件
            toolbarSubwayMain.textView7.setOnClickListener {
                JDCityPicker().showJDPicker(it.context, { pro, city, disc ->
                    toolbarSubwayMain.textView7.text = "${pro}${city}${disc}"
                }, {
                    ToastUtils.showShort("请选择地区")
                })
            }
            subwayWeatherBanner.run {
                setOrientation(Banner.VERTICAL)
                setPageTransformer(ZoomOutPageTransformer())
            }
            typeSubwayServiceBar.typeBarText.text = "栏目"
            typeWeatherBar.typeBarText.text = "天气信息"
            typeSubwayLineHomeInfo.typeBarText.text = "站点信息"
            rvSubwayService.run {
                layoutManager = GridLayoutManager(this.context, 4)
                adapter = subWayServiceAdapter
            }
            rvSubwayCurrentStation.apply {
                adapter = subHomeAdapter
            }
        }
    }

    override fun addObservers(bindView: FragmentSubwayHomeBinding) {
        subwayViewModel.homeBanner.observe(viewLifecycleOwner) {
            if (it is Resource.Success) {
                it.data?.let {
                    bindView.bannerSubway.run {
                        setAdapter(object : BannerImageAdapter<String>(it) {
                            override fun onBindView(
                                holder: BannerImageHolder?,
                                data: String?,
                                position: Int,
                                size: Int
                            ) {
                                holder?.imageView?.onLoad(data ?: "")
                            }

                        })
                    }
                }

            }
        }
        subwayViewModel.locationInfo.observe(viewLifecycleOwner) {
            if (it is Resource.Success) {
                bindView.toolbarSubwayMain.textView7.text = it.data
            }
        }
        subwayViewModel.weatherInfo.observe(viewLifecycleOwner) {
            if (it is Resource.Success) {
                it.data?.run {
                    val weatherList = mutableListOf<WeatherBannerBean>(
                        WeatherBannerBean(
                            "温度",
                            "实时温度${temperature}°C 平均温度$apparentTemperature°C",
                            "最高温度${maxTemperature}°C 最低气温${minTemperature}°C"
                        ),
                        WeatherBannerBean(
                            "天气", "天气：${weather} 紫外线：$uv", "湿度：${humidity}%RH 空气质量：$air"
                        )
                    )
                    val weatherBannerAdapter = WeatherBannerAdapter(weatherList)
                    bindView.subwayWeatherBanner.setAdapter(weatherBannerAdapter)
                }

            }
        }

        subwayViewModel.homeStationInfo.observe(viewLifecycleOwner) {
            Log.d(TAG, "addObservers: $it")
            when (it) {
                is Resource.Success ->
                    subHomeAdapter.setDiffNewData(it.data!!.toMutableList())
            }

        }
    }
}